package 我的Java学习_算法基础.day_06;

import java.util.Scanner;

public class _099_一维世界的爱情_青蛙的约会 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextInt();
        long y = sc.nextInt();
        long m = sc.nextInt();
        long n = sc.nextInt();
        long L = sc.nextInt();
        long a = m-n;
        long b = L;
        m=y-x;
        long d = 0;
        try{
            d= LinearEquation.linearEquation(a,b,m);//求线性方程
            long x0 = LinearEquation.x;
            System.out.println(x0);
            b/=d;//这时b是b/d ,即通解为x0+k*b
            b=Math.abs(b);//相当于一个正周期，x0加整数倍b都是解
            //关键
            x0 = (x0%b+b)%b;//求大于0的第一个解
            System.out.println(x0);
        }catch (Exception e){
            System.out.println("Impossible");
        }
    }
    private static class LinearEquation {
        static long x;
        static long y;
        /**
         * 扩展欧几里得
         * 调用完成后x、y是ax+by=gcd(a,b)的解
         * @param a
         * @param b
         * @return 最大公约数
         */
        public static long ext_gcd(long a,long b){
            if(b==0){
                x=1;
                y=0;
                return a;
            }
            long res = ext_gcd(b,a%b);
            //x,y已经被下一层递归更新了
            long x1 = x;//备份x
            x=y;//更新x
            y=x1-a/b*y;//更新y
            return res;
        }

        /**
         * 求ax+by=m 的解
         * @param a
         * @param b
         * @param m
         * @throws Exception
         */
        public static long linearEquation(long a,long b,long m)throws Exception{
            long d = ext_gcd(a,b);
            //m不是gcd（a,b）的倍数，这个方程无解
            if(m%d!=0) throw new Exception("无解");
            long n = m/d;
            x*=n;
            y*=n;
            return d;
        }
    }

}
